package com.efast.cafe.portal.controller.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.base.BaseCompanyInfoBean;
import com.efast.cafe.portal.bean.company.PortalCompanyOrgBean;
import com.efast.cafe.portal.bean.company.PortalCompanyPositionBean;
import com.efast.cafe.portal.entity.base.PortalBaseParameter;
import com.efast.cafe.portal.entity.company.PortalCompanyOrg;
import com.efast.cafe.portal.entity.company.PortalCompanyPosition;
import com.efast.cafe.portal.remote.common.ParameterService;
import com.efast.cafe.portal.service.base.IBaseCompanyInfoService;
import com.efast.cafe.portal.service.company.IPortalCompanyOrgService;
import com.efast.cafe.portal.service.company.IPortalCompanyPositionService;
import com.netflix.discovery.CommonConstants;

/**
 * ****************************************************************************
 * 
 * @File name : PortalCompanyOrgController.java
 *
 * @Author : bbqu
 *
 * @Date : 2016-8-11
 *
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-8-11 下午3:48:10 bbqu 1.0
 *            Initial Version
 ****************************************************************************
 */
@Controller
@RequestMapping("/frame_center/company")
public class PortalCompanyOrgController extends AbstractController {
	@Autowired
	private IPortalCompanyOrgService portalCompanyOrgService;
	@Autowired
	private IBaseCompanyInfoService baseCompanyInfoService;
	@Autowired
	private IPortalCompanyPositionService portalCompanyPositionService;
	@Autowired
	private ParameterService parameterService;

	/**
	 * 跳转到list页面
	 * 
	 * @Date : 2016-8-11
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/org/init")
	public @ResponseBody ResultBean infoPage(@RequestBody ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		ResultBean result = new ResultBean(true, "");
		try {
			BaseCompanyInfoBean companyInfo = baseCompanyInfoService.queryByCode();
			PortalCompanyOrg portalCompanyOrg = new PortalCompanyOrg();
			portalCompanyOrg.setPortal_company_org_id(0L);
			portalCompanyOrg.setOrg_name(companyInfo.getCompanyName_zh_CN());
			portalCompanyOrg.setOrg_code(companyInfo.getCompanyCode());
			portalCompanyOrg.setOrg_type(companyInfo.getCompanyType().toString());
			List<PortalCompanyOrgBean> listResult = new ArrayList<>();
			PortalCompanyOrgBean portalCompanyOrgBean = new PortalCompanyOrgBean();
			portalCompanyOrgBean.setData(portalCompanyOrg);
			portalCompanyOrgBean.setTitle(portalCompanyOrg.getOrg_name());
			listResult.add(portalCompanyOrgBean);
			result.setObject(listResult);

			// List<PortalCompanyOrgBean> listResult=new ArrayList<>();
			List<PortalCompanyOrg> listpoPortalCompanyOrgList = portalCompanyOrgService.queryPortalCompanyOrgList();
			for (int i = 0; i < listpoPortalCompanyOrgList.size(); i++) {
				PortalCompanyOrgBean portalCompanyOrgBean1 = new PortalCompanyOrgBean();
				portalCompanyOrgBean1.setTitle(listpoPortalCompanyOrgList.get(i).getOrg_name());
				portalCompanyOrgBean1.setData(listpoPortalCompanyOrgList.get(i));
				listResult.add(portalCompanyOrgBean1);
			}
			result.setObject(listResult);

			/*
			 * PortalCompanyOrg
			 * parentOrg=getOrgTree(listpoPortalCompanyOrgList);
			 * parentOrg.setTitle(companyInfo.getCompanyName_zh_CN());
			 * result.setObject(parentOrg);
			 */
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		/*
		 * Map map = new HashMap(); map.put("org_name",
		 * companyInfo.getCompanyName_zh_CN()); map.put("parent_id", -1);
		 * map.put("portal_company_org_id", 0);
		 * 
		 * List list = new ArrayList(); list.addAll(listpoPortalCompanyOrgList);
		 * list.add(map); //return list; model.addAttribute("list",new
		 * JSONArray(list)); model.addAttribute("listpoPortalCompanyOrgList",new
		 * JSONArray(listpoPortalCompanyOrgList)); return "/company/org/list";
		 */
		return result;
	}

	/**
	 * 通过id查询组织
	 * 
	 * @Date : 2016-8-12
	 * @param portalCompanyOrgId
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/org/companyorgform")
	public @ResponseBody ResultBean queryCompanyOrgById(@RequestParam(required = false) Long portalCompanyOrgId,
			ModelMap modelMap) {
		ResultBean result = new ResultBean(true, "");
		PortalCompanyOrg portalCompanyOrg = portalCompanyOrgService.queryCompanyOrgById(portalCompanyOrgId);
		result.setObject(portalCompanyOrg);
		return result;
	}

	/**
	 * 修改companyOrg
	 * 
	 * @Date : 2016-8-12
	 * @param portalCompanyOrg
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/org/edit")
	public @ResponseBody ResultBean updateCompanyOrg(@RequestBody PortalCompanyOrg portalCompanyOrg,
			ModelMap modelMap) {

		ResultBean result = new ResultBean(true, null);
		try {
			Long parentId = portalCompanyOrgService.queryCompanyOrgById(portalCompanyOrg.getPortal_company_org_id())
					.getParent_id();
			portalCompanyOrgService.updateCompanyOrg(portalCompanyOrg);
			result.setObject(parentId);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;

	}

	/**
	 * 跳转到新增Companyorg页面
	 * 
	 * @Date : 2016-8-12
	 * @param portalCompanyOrgId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/org/form")
	public String formPage(@RequestParam(required = false) Long portalCompanyOrgId,
			/* @RequestParam(required = false)Long parentId, */
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		PortalCompanyOrg portalCompanyOrg = null;
		if (portalCompanyOrgId != null) {
			portalCompanyOrg = portalCompanyOrgService.queryCompanyOrgById(portalCompanyOrgId);
		} else {
			portalCompanyOrg = new PortalCompanyOrg();
		}

		model.addAttribute("portalCompanyOrg", portalCompanyOrg);
		return "/company/org/form";
	}

	/**
	 * 新增
	 * 
	 * @Date : 2016-8-12
	 * @param portalCompanyOrg
	 * @param request
	 * @param response
	 * @param modelAndView
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/org/submit")
	public @ResponseBody ResultBean insertCompanyOrg(@RequestBody PortalCompanyOrg portalCompanyOrg,
			HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView, ModelMap model) {
		ResultBean result = new ResultBean(true, null);
		try {
			portalCompanyOrgService.insertCompanyOrg(portalCompanyOrg);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}

	@RequestMapping(value = "/org/delete")
	public @ResponseBody ResultBean deleteCompanyOrg(@RequestBody ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		Long portalCompanyOrgId = null;
		if (model.containsKey("portalCompanyOrgId")) {
			portalCompanyOrgId = Long.parseLong(model.get("portalCompanyOrgId").toString());

		}
		List<PortalCompanyPosition> listPortalCompanyPositions = portalCompanyPositionService
				.queryPositionByOrgId(portalCompanyOrgId);
		List<PortalCompanyOrg> listpoPortalCompanyOrgList = portalCompanyOrgService
				.queryPortalCompanyOrgList(Integer.parseInt(portalCompanyOrgId.toString()));

		ResultBean result = new ResultBean(true, null);
		try {
			if (listpoPortalCompanyOrgList != null && listpoPortalCompanyOrgList.size() != 0) {
				throw new ServiceException("含有子节点不能删除");
			} else if (listPortalCompanyPositions != null && listPortalCompanyPositions.size() != 0) {
				throw new ServiceException("含有职位");
			}
			Long parentId = portalCompanyOrgService.queryCompanyOrgById(portalCompanyOrgId).getParent_id();
			portalCompanyOrgService.deleteCompanyOrg(portalCompanyOrgId);
			result.setObject(parentId);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}

	@RequestMapping(value = "/org/tree")
	public @ResponseBody List<PortalCompanyOrg> treePage(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		List<PortalCompanyOrg> listpoPortalCompanyOrgList = portalCompanyOrgService.queryPortalCompanyOrgList();
		BaseCompanyInfoBean companyInfo = baseCompanyInfoService.queryByCode();

		model.addAttribute("listpoPortalCompanyOrgList", new JSONArray(listpoPortalCompanyOrgList));

		Map map = new HashMap();

		map.put("org_name", companyInfo.getCompanyName_zh_CN());
		map.put("parent_id", -1);
		map.put("portal_company_org_id", 0);
		List list = new ArrayList();
		list.addAll(listpoPortalCompanyOrgList);
		list.add(map);
		return list;
	}

	/**
	 * 初始化组织职位结构树
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年9月26日
	 */
	@RequestMapping(value = "/org/position/init")
	public @ResponseBody ResultBean initOrgTree(@RequestBody ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		ResultBean result = new ResultBean(true, "");
		try {
			BaseCompanyInfoBean companyInfo = baseCompanyInfoService.queryByCode();
			PortalCompanyOrg portalCompanyOrg = new PortalCompanyOrg();
			portalCompanyOrg.setPortal_company_org_id(0L);
			portalCompanyOrg.setOrg_name(companyInfo.getCompanyName_zh_CN());
			portalCompanyOrg.setOrg_code(companyInfo.getCompanyCode());
			portalCompanyOrg.setOrg_type(companyInfo.getCompanyType().toString());
			List<PortalCompanyOrgBean> listResult = new ArrayList<>();
			PortalCompanyOrgBean portalCompanyOrgBean = new PortalCompanyOrgBean();
			portalCompanyOrgBean.setData(portalCompanyOrg);
			portalCompanyOrgBean.setTitle(portalCompanyOrg.getOrg_name());
			listResult.add(portalCompanyOrgBean);
			result.setObject(listResult);

			// 初始化组织
			List<PortalCompanyOrg> listpoPortalCompanyOrgList = portalCompanyOrgService.queryPortalCompanyOrgList();
			for (int i = 0; i < listpoPortalCompanyOrgList.size(); i++) {
				PortalCompanyOrgBean portalCompanyOrgBean1 = new PortalCompanyOrgBean();
				portalCompanyOrgBean1.setTitle(listpoPortalCompanyOrgList.get(i).getOrg_name());
				portalCompanyOrgBean1.setData(listpoPortalCompanyOrgList.get(i));
				listResult.add(portalCompanyOrgBean1);
			}
			// 获取参数组织机构中职位是否启用
			PortalBaseParameter paramValue = parameterService
					.api_getParameter(CommonConstant.ParameterKeys.COMPANY_POSITION_ENABLE, "Global");
			boolean isEnablePosition = true;
			if (null != paramValue) {
				isEnablePosition = Boolean.parseBoolean(paramValue.getParam_value());
			}
			if (isEnablePosition) {
				List<PortalCompanyPositionBean> positionList = portalCompanyPositionService.queryPositionOrgId(null);
				for (PortalCompanyPositionBean position : positionList) {
					PortalCompanyOrgBean positionNodeBean = new PortalCompanyOrgBean();
					positionNodeBean.setType(1);
					positionNodeBean.setTitle(position.getPosition_name());
					positionNodeBean.setData(position);
					listResult.add(positionNodeBean);
				}
				result.setObject(listResult);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}

}
